package com.wenhuitec.android.tang.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSONArray;
import com.dhkj.web.bean.AddrTree;
import com.dhkj.web.bean.Bmzl;
import com.dhkj.web.bean.Zweekmenubak;
import com.wenhuitec.android.tang.R;
import com.wenhuitec.android.tang.config.SystemInit;
import com.wenhuitec.android.tang.config.SystemParam;
import com.wenhuitec.android.tang.sysui.model.CarFoodsModel;
import com.wenhuitec.android.tang.sysui.model.PickModel;
import com.wenhuitec.android.tang.sysui.view.PickView;
import com.wenhuitec.android.tang.ui.adapter.FoodsGvAdapter;
import com.wenhuitec.android.tang.ui.adapter.CarFoodsAdapter;
import com.wenhuitec.android.tang.ui.adapter.SelectFoodsAdapter;
import com.wenhuitec.android.tang.ui.dialog.MsgDialog;
import com.wenhuitec.android.tang.ui.dialog.PaySelectDialog;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class Activity2 extends AppCompatActivity {

    private PickView addressPv;
    private PickView shopPv;
    private PickView zlPv;

    private Context mContext;
    private GridView mGridView;
    private RecyclerView foodsRv;
    private RecyclerView recyclerView;

    private TextView amountCountTv;
    private TextView amountPriceTv;

    private List<AddrTree> addrTreeList;
    private Map<Integer, List<Bmzl>> mBmzlMap;
    private Map<Integer, List<Zweekmenubak>> zWeekMenuBakMap;

    private String addr;
    private String bmzl;
    private CarFoodsAdapter carFoodsAdapter;
    private SelectFoodsAdapter selectFoodsAdapter;
    private Button clearBut;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        mContext = this;
        addrTreeList = SystemInit.getInstance(mContext).getAddrTreeList();
        mBmzlMap = SystemInit.getInstance(mContext).getBmzlList();
        zWeekMenuBakMap = SystemInit.getInstance(mContext).getzWeekMenuBakMap();
        addressPv = findViewById(R.id.addressPv);
        shopPv = findViewById(R.id.shopPv);
        zlPv = findViewById(R.id.zlPv);
        foodsRv = findViewById(R.id.foodsGv);
        amountCountTv = findViewById(R.id.amountCountTv);
        amountPriceTv = findViewById(R.id.amountPriceTv);
        clearBut = findViewById(R.id.clearBut);
        recyclerView = findViewById(R.id.carFoodsRv);
        selectFoodsAdapter = new SelectFoodsAdapter(this);
        findViewById(R.id.toPayBut).setOnClickListener(v -> {
            Intent intent = new Intent(mContext, PaySelectDialog.class);
            List<CarFoodsModel> carFoodsModels = carFoodsAdapter.getList();
            intent.putExtra("data", JSONArray.toJSONString(carFoodsModels));
            startActivityForResult(intent, 1011);
        });
        carFoodsAdapter = new CarFoodsAdapter(mContext);
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(carFoodsAdapter);
        carFoodsAdapter.setCarFoodsAmountListener((amountPrice, amountCount) -> {
            amountCountTv.setText("数量：" + amountCount.stripTrailingZeros().toPlainString() + " 份");
            amountPriceTv.setText("合计：" + amountPrice.stripTrailingZeros().toPlainString() + "  元");
        });
        clearBut.setOnClickListener(v -> {
            MsgDialog msgDialog = new MsgDialog();
            msgDialog.show(mContext, "是否清空", new MsgDialog.OnBottomClickListener() {
                @Override
                public void positive() {
                    carFoodsAdapter.clearCar();
                }

                @Override
                public void negative() {

                }
            });

        });
        initAddress();
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1011 && resultCode == RESULT_OK) {
            carFoodsAdapter.clearCar();
        }
    }

    private void initAddress() {
        addressPv.setOnClick((pickModel, position) -> {
            if (carFoodsAdapter.getList().size() > 0) {
                MsgDialog msgDialog = new MsgDialog();
                msgDialog.show(mContext, "切换商铺后，所选的菜将会清空，是否需要切换商铺", new MsgDialog.OnBottomClickListener() {
                    @Override
                    public void positive() {
                        carFoodsAdapter.clearCar();
                        shopPv.setSelectPot(-1);
                        zlPv.setRv(new ArrayList<>());
                        SystemParam.getInstance().setAddr1(position);
                        SystemParam.getInstance().setAddr2(-1);
                        initShop(pickModel.getId());
                        selectFoodsAdapter.setList(new ArrayList<>());
                    }

                    @Override
                    public void negative() {
                        addressPv.setSelectNoClick(SystemParam.getInstance().getAddr1());
                    }
                });
                addressPv.setSelectNoClick(SystemParam.getInstance().getAddr2());
                return;
            }
            zlPv.setRv(new ArrayList<>());
            SystemParam.getInstance().setAddr1(position);
            SystemParam.getInstance().setAddr2(-1);
            initShop(pickModel.getId());
            selectFoodsAdapter.setList(new ArrayList<>());
        });
        addressPv.setTitle("地点：");
        List<PickModel> pickModelList = new ArrayList<>();
        for (AddrTree goo : addrTreeList) {
            pickModelList.add(new PickModel(String.valueOf(goo.getId()), goo.getAddrName().trim()));
        }
        addressPv.setRv(pickModelList);
        addressPv.setSelectPot(SystemParam.getInstance().getAddr1());
    }

    private void initShop(String id) {
        shopPv.setTitle("商铺：");
        shopPv.setOnClick((pickModel, position) -> {
//            if (carFoodsAdapter.getList().size() > 0) {
//                MsgDialog msgDialog = new MsgDialog();
//                msgDialog.show(mContext, "切换商铺后，所选的菜将会清空，是否需要切换商铺", new MsgDialog.OnBottomClickListener() {
//                    @Override
//                    public void positive() {
//                        carFoodsAdapter.clearCar();
//                        addr = pickModel.getId();
//                        selectFoodsAdapter.setList(new ArrayList<>());
//                        zlPv.setSelectPot(-1);
//                        initZl(pickModel.getId());
//                        SystemParam.getInstance().setAddr2(position);
//                    }
//
//                    @Override
//                    public void negative() {
//                        shopPv.setSelectNoClick(SystemParam.getInstance().getAddr2());
//                    }
//                });
//                shopPv.setSelectNoClick(SystemParam.getInstance().getAddr2());
//                return;
//            }
            if (SystemParam.getInstance().getAddr2() != position) {
                selectFoodsAdapter.setList(new ArrayList<>());
                zlPv.setSelectPot(-1);
            }
            addr = pickModel.getId();
            initZl(pickModel.getId());
            SystemParam.getInstance().setAddr2(position);
        });
        AddrTree addrTree = getAddrTree(addrTreeList, id);
        List<PickModel> pickModelList = new ArrayList<>();
        if (addrTree != null) {
            for (AddrTree goo : addrTree.getChildren()) {
                pickModelList.add(new PickModel(String.valueOf(goo.getId()), goo.getAddrName().trim()));
            }
        }
        shopPv.setRv(pickModelList);
        shopPv.setSelectPot(SystemParam.getInstance().getAddr2());

    }

    private AddrTree getAddrTree(List<AddrTree> addrTreeList, String id) {
        for (AddrTree goo : addrTreeList) {
            if (goo.getId().equals(id)) {
                return goo;
            }
        }
        return null;
    }


    private void initZl(String id) {
        zlPv.setTitle("菜式：");
        List<Bmzl> bmzlList = mBmzlMap.get(Integer.parseInt(id));
        List<PickModel> pickModelList = new ArrayList<>();
        if (bmzlList != null) {
            for (Bmzl goo : bmzlList) {
                pickModelList.add(new PickModel(goo.getBm(), goo.getName().trim()));
            }
        }
        zlPv.setRv(pickModelList);
        if (pickModelList.size() > 0) {
            zlPv.setSelectPot(0);
            bmzl = pickModelList.get(0).getId();
            initFoods();
        }
        zlPv.setOnClick((pickModel, position) -> {
            //按钮点击
            bmzl = pickModel.getId();
            initFoods();
        });
    }

    //菜品布局
    private void initFoods() {
        List<Zweekmenubak> zweekmenubakList = new ArrayList<>();
        List<Zweekmenubak> zweekmenubakList1 = zWeekMenuBakMap.get(Integer.parseInt(addr));

        if (zweekmenubakList1 != null) {
            zweekmenubakList = zweekmenubakList1.stream().filter(u -> u.getZl().trim().equals(bmzl.trim())).collect(Collectors.toList());

        }

        GridLayoutManager layoutManager = new GridLayoutManager(this, 4);
        layoutManager.setOrientation(RecyclerView.VERTICAL);
        foodsRv.setLayoutManager(layoutManager);

        foodsRv.setAdapter(selectFoodsAdapter);
        selectFoodsAdapter.setList(zweekmenubakList);
        selectFoodsAdapter.setmGvOnClick(zweekmenubak -> {
            carFoodsAdapter.addFoods(zweekmenubak);
        });

    }


}